<?php
namespace app\manage\controller;

use app\manage\model\AdminUser;
use app\manage\model\UserRole;
use think\Controller;
use think\migration\command\migrate\Status;
use think\Request;
use think\Validate;
use think\Db;
use Config;
use app\common\model\TokenCheck;
use app\manage\model\OperationLog;
use app\manage\model\Department as m_Department;

class Department extends Controller{
    public function __construct(Request $request = null)
    {
//        parent::__construct($request);
    }

    /**
     * 显示部门
     * @param   keyword         关键字
     * @param   p               页码
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function showDepartment() {
        $input = input();
        $keyword = isset($input['keyword']) ? trim($input['keyword']) : "";
        $page = isset($input['p']) ? trim($input['p']) : "";
        $create_id = isset($input['admin_id']) ? trim($input['admin_id']) : "";

        /**
         * 获取一页数据和总数，尚未进行错误判断与处理
         */
        $model=new m_Department();
        $create_id = $model->findPlatId($create_id);
        $onePage = $model->onePage($page, get_list_rows(), $keyword, $create_id);
        $total = $model->total($keyword, $create_id);

        $data['total'] = $total;
        $data['data'] = $onePage;
        returnJson(200,"成功", $data)->send();die;
    }

    /**
     * 增加部门
     * @param   name    部门名称
     * @param   code    部门编号
     */
    public function createDepartment() {
        $input = input();
        $validate = new Validate([
            ['name', 'require|max:50', '名称不能为空|不能超过20字'],
            ['code', 'require|max:50', '编号不能为空|不能超过20字']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $data['name'] = isset($input['name']) ? trim($input['name']) : "";
        $data['code'] = isset($input['code']) ? trim($input['code']) : "";
        $data['create_id'] = isset($input['admin_id']) ? trim($input['admin_id']) : "";
        $data['number'] = 0;
        $data['status'] = 1;
        $data['create_time'] = time();
        $data['update_time'] = time();

        /**
         * 检查是否已存在名称或编号
         */
        $model=new m_Department();
        $data['create_id'] = $model->findPlatId($data['create_id']);
        if (($model->uniqueness($data['name'], $data['code'], $data['create_id'])) !== 0) {
            returnJson(-2012,"部门名或部门编号已存在")->send();die;
        }

        $addtextRes=$model->m_insert($data);
        if ($addtextRes) {
            returnJson(200,"成功", $addtextRes)->send();die;
        } else {
            returnJson(-2001,"服务器错误")->send();die;
        }
    }

    /**
     * 更新平台账户
     * @param   department_id
     * @param   name    部门名称
     * @param   code    部门编号
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function editDepartment() {
        $input = input();
        $validate = new Validate([
            ['department_id', 'require', '平台id不能为空'],
            ['name', 'require|max:50', '名称不能为空|不能超过20字'],
            ['code', 'require|max:50', '编号不能为空|不能超过20字']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $department_id = isset($input['department_id']) ? trim($input['department_id']) : "";
        $data['name'] = isset($input['name']) ? trim($input['name']) : "";
        $data['code'] = isset($input['code']) ? trim($input['code']) : "";
        $data['update_time'] = time();

        $model=new m_Department();
        $edittextRes=$model->m_update($department_id, $data);
        if ($edittextRes) {
            returnJson(200,"成功")->send();die;
        } else {
            returnJson(-2013,"数据无变化")->send();die;
        }
    }

    /**
     * @param   department_id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function deleteDepartment() {
        $input = input();
        $validate = new Validate([
            ['department_id', 'require', '平台id不能为空']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $department_id = isset($input['department_id']) ? trim($input['department_id']) : "";

        /**
         * 查看是否有隶属于该部门的用户
         */
        $model=new m_Department();
        $count = $model->detail($department_id);
        if ($count['number'] !== 0) {
            returnJson(-2001,"存在属于该部门的用户，请先删除")->send();die;
        }

        $data['status'] = 3;
        $deletetextRes=$model->m_update($department_id, $data);
        if ($deletetextRes) {
            returnJson(200,"成功")->send();die;
        }
    }

    /**
     * @param   department_id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detailDepartment() {
        $input = input();
        $validate = new Validate([
            ['department_id', 'require', '平台id不能为空']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $department_id = isset($input['department_id']) ? trim($input['department_id']) : "";

        $model=new m_Department();
        $detailtextRes=$model->detail($department_id);
        if (!$detailtextRes) {
            returnJson(-2001,"服务器错误")->send();die;
        }

        /**
         * 查看该部门下所有员工信息
         */
        $model = new AdminUser();
        $detail = $model->getDataDepartment($department_id);
        // 查找每一个用户的角色，根据角色表知道是否是部门管理员
        $model = new \app\manage\model\Role();
        $model1 = new UserRole();
        foreach ($detail as $k => $value) {
            $role_info = $model1->getRoleRelation($value['admin_id']);
            $role_info = $model->detail($role_info[0]['role_id']);
            $detail[$k]['is_admin'] = $role_info['is_department'];
        }

        $detailtextRes['member'] = $detail;
        returnJson(200,"成功", $detailtextRes)->send();die;
    }

}
